package com.qianyu.core.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianyu.core.api.ApiCommon;
import com.qianyu.core.entity.bean.*;
import com.qianyu.core.entity.dto.LinkDto;
import com.qianyu.core.entity.dto.api.ClaimQBActivityDto;
import com.qianyu.core.helper.LoginHelper;
import com.qianyu.core.mapper.CronTaskMapper;
import com.qianyu.core.mapper.LinkActMapper;
import com.qianyu.core.mapper.LinkMapper;
import com.qianyu.core.service.CornTaskItemService;
import com.qianyu.core.service.GameAreaService;
import com.qianyu.core.service.LinkActService;
import com.qianyu.core.service.QqAccountService;
import com.qianyu.core.util.CacheKeyUtil;
import com.qianyu.core.util.RedisUtil;
import com.qianyu.core.util.TimeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
@RequiredArgsConstructor
public class LinkActServiceImpl extends ServiceImpl<LinkActMapper, LinkAct> implements LinkActService {

    private final ApiCommon apiCommon;

    private final QqAccountService qqAccountService;

    private final GameAreaService gameAreaService;

    private final LinkMapper linkMapper;

    private final RedisUtil redisUtil;

    private final CronTaskMapper cronTaskMapper;

    private final CornTaskItemService cornTaskItemService;


    @Override
    public boolean joinActivity(LinkDto.LinkActDto dto) {

        Collection<QqAccount> accounts = qqAccountService.listByIds(dto.getQqAccountIds());
        LinkAct linkAct = baseMapper.selectById(dto.getLinkActId());
        Link link = linkMapper.selectById(linkAct.getLinkId());
        GameArea gameArea = gameAreaService.getById(dto.getGameAreaId());
        CronTask task = new CronTask() {{
            setAppid(link.getAppid());
            setLinkId(link.getId());
            setActId(linkAct.getId());
            setAreaId(gameArea.getId());
            setUserId(LoginHelper.getUserId());
            setAccountNum(accounts.size());
            setTaskName(linkAct.getActName());
            setType(0);
            setExecStatus(2);
            setStartTime(TimeUtil.unixTime());
            setEndTime(TimeUtil.unixTime());
        }};
        cronTaskMapper.insert(task);
        List<CronTaskItem> taskItems = new ArrayList<>();
        for (QqAccount account : accounts) {
            ClaimQBActivityDto activityDto = new ClaimQBActivityDto();
            activityDto.setCache(account.getCache());
            String proxy = account.getProxy1();
            String[] split = proxy.split("-");
            activityDto.setProxyConfig(new ClaimQBActivityDto.ProxyConfig() {{
                setIp(split[0]);
                setPort(split[1]);
                setUser(split[2]);
                setPass(split[3]);
            }});
            activityDto.setGameConfig(new ClaimQBActivityDto.GameConfig() {{
                setActId(String.valueOf(linkAct.getActId()));
                setActType(0);
                setActtk(link.getToken());
                setServiceName(gameArea.getArea());
                setGameName(link.getAppName());
                setGame(link.getGame());
                setIsAuthRole(false);
                setNeedSelectArea(true);
            }});
            JSONObject jsonObject = apiCommon.claimQBActivity(activityDto, account.getQq(), 0);
            if (jsonObject.getInteger("code") == 200) {
                redisUtil.lSet(CacheKeyUtil.flushQbSet(task.getId()), account);
            }
            CronTaskItem item = new CronTaskItem() {{
                setAccountId(account.getId());
                setActId(linkAct.getId());
                setAreaId(gameArea.getId());
                setUserId(LoginHelper.getUserId());
                setTaskId(task.getId());
            }};
            taskItems.add(item);

        }
        cornTaskItemService.saveBatch(taskItems);
        return true;
    }
}
